#include <stdio.h>
#include <string.h>
#include <StdFile.h>
#include <unistd.h>
#include "StdTcp.h"
#include "StdThread.h"
#include "DoubleLinkList.h"
#include "../GlobalMessage.h"
#define PORT 8080
#define IP "192.168.204.128"

char account[NameSize];


void Login(TcpC *c)
{
    GLMSG msg;
    msg.flag = login;
    printf("请输入你的账号:");
    scanf("%s",msg.fromName);
    printf("%s\n",msg.fromName);
    while(getchar() != '\n');
    strcpy(account,msg.fromName);
    TcpClientSend(c,&msg,sizeof(msg));
}

void* Thread_Read(void *arg)
{
    TcpC *c = (TcpC*)arg;
    while(1)
    {
        GLMSG m;
        
        
        if(TcpClientRecv(c,&m,sizeof(m)) == false)
        {
            printf("与服务器断开连接!\n");
            exit(-1);
        }
        if(m.flag == FileStart)
        {
            WriteToFile(m.pathName,m.content,strlen(m.content));
        }
        else if(m.flag == sendFile)
        {
            AppendToFile(m.pathName,m.content,strlen(m.content));
        }
        else
        {
            printf("接收到来自|%s|的消息 : %s\n",m.fromName,m.content);
        }
    }
}

void ChooseSelect(GLMSG *m,TcpC *c)
{
    printf("请输入选项:1.私聊 2.群聊 3.发送文件:\n");
    int choice;
    scanf("%d",&choice);
    while(getchar() != '\n');
    switch (choice)
    {
    case chat:
        m->flag = chat;
        printf("输入你要发消息的账号:");
        scanf("%s",m->toName);
        while(getchar() != '\n');
        printf("请输入你要发的内容:");
        scanf("%s",m->content);
        while(getchar() != '\n');
        TcpClientSend(c,m,sizeof(GLMSG));
        break;
    
    case allchat:
        m->flag = allchat;
        printf("请输入你要群发的内容:");
        scanf("%s",m->content);
        while(getchar() != '\n');
        TcpClientSend(c,m,sizeof(GLMSG));
        break;
    case sendFile:
        
        printf("请输入你要发送的文件路径:");
        char path[512] = {0};
        scanf("%s",path);
        printf("输入你要发送文件的账号:");
        scanf("%s",m->toName);
        while(getchar() != '\n');


        //获取路径文件名
        INITSTERSTRING(pathList);
        pathList = Split(path,"/");
        sprintf(m->pathName,"%s",pathList.StringList[pathList.len - 1].string);

        FILE *f = fopen(path,"r+");
        if(f == NULL)
        {
            printf("文件打开失败!\n");
            return;
        }


        
        //发送第一包文件——单独发
        m->flag = FileStart;
        fread(m->content,1,sizeof(m->content),f);
        TcpClientSend(c,m,sizeof(GLMSG));

        //后面的文件发送
        m->flag = sendFile;
        while(feof(f) == 0)
        {
            memset(m->content,0,sizeof(m->content));
            fread(m->content,1,sizeof(m->content),f);
            TcpClientSend(c,m,sizeof(GLMSG));
        }
        fclose(f);
        break;
    default:
        break;
    }
    
}

void SendMsg(TcpC *c)
{
    while(1)
    {
        GLMSG m;
        strcpy(m.fromName,account);
        ChooseSelect(&m,c);
        
    }
}

int main()
{
    TcpC *c = InitTcpClient(IP,PORT);
    Login(c);//登陆
    Thread *readT = InitThread(Thread_Read,c);
    SendMsg(c);

    // //套接字
    // int sock = socket(AF_INET,SOCK_STREAM,0);
    // if(sock < 0)
    // {
    //     perror("socket");
    //     return -1;
    // }

    // struct sockaddr_in addr;
    // addr.sin_family = AF_INET;
    // addr.sin_port = htons(PORT);
    // addr.sin_addr.s_addr = inet_addr(IP);
    
    // //连接服务器函数
    // if(connect(sock,(struct sockaddr*)&addr,sizeof(addr)) < 0)
    // {
    //     perror("connect");
    //     return -1;
    // }
    // printf("客户端连接成功!\n");

    // while(1)
    // {
    //     char msg[1024] = {0};
    //     scanf("%s",msg);
    //     while(getchar() != '\n');
    //     //send(sock,msg,1024,0);
    //     TcpClientSend(c,msg,1024);
    // }

    return 0; 
}